﻿$(function () {
    var code = $.query.get("code");
    $("#hidCode").val(code);
    ///绑定页面方法
    function SetEditorContents(EditorName, ContentStr) { var oEditor = CKEDITOR.instances[EditorName]; oEditor.setData(ContentStr); }

    var BindData = function () {
        if (code != "-1") {
            $.blockUI({ message: "<h2>数据加载中.......</h2>", css: { width: "300px", height: "50px"} });
            $.post("../AjaxHandler/TourismInfoEditHandler.ashx?t=" + new Date(), { action: "Get", hidCode: code }, function (data) {
                $("#title").val(data.model.Title);
                $("#subtitle").val(data.model.SubTitle);
                $("#tags").val(data.model.Tags);
                $("#country").val(data.model.Region.substring(0, data.model.Region.toString().indexOf("-")))
                $("#country").trigger("change", data.model.Region.substring(data.model.Region.toString().indexOf("-") + 1, data.model.Region.toString().lastIndexOf("-")));
                $("#province").val(data.model.Region.substring(data.model.Region.toString().indexOf("-") + 1, data.model.Region.toString().lastIndexOf("-")))
                $("#province").trigger("change", data.model.Region.substring(data.model.Region.toString().lastIndexOf("-") + 1));
                $("#city").val(data.model.Region.substring(data.model.Region.toString().lastIndexOf("-") + 1));
                $("#city").trigger("change");
                $("#tourtype").val(data.model.TypeCode);
                if (data.model.IsSpecially != "" && data.model.IsSpecially != null) {
                    $("input[name=isSpecial][value='" + data.model.IsSpecially + "']").attr("checked", "checked");
                }
                SetEditorContents("desc", data.model.Desc);
                SetEditorContents("remark", data.model.Remark);
                SetEditorContents("recommand", data.model.SpecialRecommand);

                var file = data.file; //获取图片
                //加载图片开始
                for (var i = 0; i < file.length; i++) {
                    $("#hidfilename").val($("#hidfilename").val() + file[i].FilePath + ",");
                    $("#hidthumbnailsname").val($("#hidthumbnailsname").val() + file[i].ThumbnailsPath + ",");
                    $("#hidpreviewname").val($("#hidpreviewname").val() + file[i].PreviewPath + ",");
                    $("#imgbroswer").find(".galleria-container").remove();
                    $("#imgbroswer").html($("#imgbroswer").html() + "<a href='" + file[i].FilePath + "' width='800px' height='300px;'><img src='" + file[i].FilePath + "' width='800px' height='300px'/></a>");
                    if (file[i].IsCover != null && file[i].IsCover) {
                        $("#iscoverspan").find("input[type=checkbox]").val(file[i].FilePath).attr("checked", "checked");
                    }
                }
                $("#imgbroswer").galleria({ linkSourceImages: false, transition: "fade", transitionSpeed: 1000, width: "500px", height: "400px" });
                //预览图片modal
                $("#imgbroswer").delegate(".galleria-images .galleria-image", "click", function () {
                    var $imgSrc = $(this).find("img").attr("src");
                    var filenamePathArray = $("#hidfilename").val().split(",");
                    var previewPathArray = $("#hidpreviewname").val().split(",");
                    var previewPath = "";
                    for (var i = 0; i < filenamePathArray.length; i++) {
                        if ($imgSrc == filenamePathArray[i]) {
                            previewPath = previewPathArray[i];
                            break;
                        }
                    }
                    $("#previewimg").html($("<div style='width:800px;margin:0px auto;'><img src='" + previewPath + "' width='800px' height='400px' style='padding:0px;position:relative;left:-7px;top:-5px;'/></a>").fadeIn()).width(800).modal();
                });
                //封面图片开始
                LoadCover();

                //加载封面图片



                //封面图片结束
                //加载图片结束

                //加载地图开始
                $("#maptitle").val(data.map.Title);
                SetEditorContents("mapdesc", data.map.Remark);
                SetEditorContents("map", data.map.Content);
                //加载地图结束
                //加载票价开始
                SetEditorContents("ticketdan", data.model.TicketDan);
                SetEditorContents("ticketwangfan", data.model.TicketWangFan);
              

                //                if (typeof (data.ticket) != "undefinde") {
                //                    if (typeof (data.ticket[1]) != "undefinde") {
                //                        $("#pricedan").val(data.ticket[1].TicketFare);
                //                        $("#priceunitdan").val(data.ticket[1].PriceUnit);
                //                        $("#seatdan").val(data.ticket[1].Seat);
                //                    }
                //                    if (typeof (data.ticket[0]) != "undefinde") {
                //                        $("#pricewangfan").val(data.ticket[0].TicketFare);
                //                        $("#priceunitwangfan").val(data.ticket[0].PriceUnit);
                //                        $("#seatwangfan").val(data.ticket[0].Seat);
                //                    }
                //                }
                //加载票价结束
                $.unblockUI();
            });
        }
    }

    ///封面处理函数
    var LoadCover = function () {
        var imgPath = "";
        $("#imgbroswer").delegate(".galleria-thumbnails-container img", "mouseover", function () {
            var p = $(this).offset();
            imgPath = $(this).attr("src");
            var $checkCover = $("#iscoverspan").find("input[type=checkbox]");
            if ($checkCover.val() != imgPath) {
                $("#iscoverspan").css("top", (p.top + 20) + "px").css("left", (p.left + 10) + "px").show().find("input[type=checkbox]").removeAttr("checked");
            } else {
                $("#iscoverspan").css("top", (p.top + 20) + "px").css("left", (p.left + 10) + "px").show().find("input[type=checkbox]").attr("checked", "checked");
            }
        });

        $("#iscoverspan").find("input[type=checkbox]").click(function () {
            if ($(this).attr("checked")) {
                $(this).val(imgPath);
            }
        })

    }


    ///绑定国家地区select函数
    var BindCountryRegion = function (sel, level, reg) {
        $.ajax({ url: "../AjaxHandler/CountryRegionSelect.ashx?t=" + new Date(), async: false, data: { region: reg, level: level }, success: function (d) {
            if (d.length > 0) {
                $("#" + sel).html("");
                $("#" + sel).append("<option value=''>--请选择--</option>");
                for (var i = 0; i < d.length; i++) {
                    $("#" + sel).append("<option value='" + d[i] + "'>" + d[i] + "</option>");
                }

            }

        }
        });

    }
    //绑定类型
    var BindTourType = function () {
        $.post("../AjaxHandler/TourismInfoTypeList.ashx?t=" + new Date(), { action: "All" }, function (d) {
            if (d.data.length > 0) {
                $("#tourtype").html("");
                $("#tourtype").append("<option value=''>--请选择--</option>");
                for (var i = 0; i < d.data.length; i++) {
                    $("#tourtype").append("<option value='" + d.data[i].Code + "'>" + d.data[i].Name + "</option>");
                }
            }
        });
    }

    BindCountryRegion("country", 0, "");
    BindTourType();
    BindData();
    //国家选择事件
    $("#country").change(function (event, provincevalue) {
        if ($(this).val() != "") {
            $("#hidcountryregion").val($(this).val());
            BindCountryRegion("province", 2, $(this).val());
            $("#province").removeAttr("disabled");

        }

        if (typeof (provincevalue) != "undefined") {
            $("#province").val(provincevalue);
        }
    });
    //省份选择事件
    $("#province").change(function (event, cityvalue) {
        if ($(this).val() != "") {
            $("#hidcountryregion").val("");
            $("#hidcountryregion").val($("#country").val() + "," + $(this).val());
            BindCountryRegion("city", 3, $(this).val());
            $("#city").removeAttr("disabled");
        }
        if (typeof (cityvalue) != "undefined") {
            $("#city").val(cityvalue);
        }
    });

    //城市选择事件          
    $("#city").change(function () {
        if ($(this).val() != "") {
            $("#hidcountryregion").val("");
            $("#hidcountryregion").val($("#country").val() + "," + $("#province").val() + "," + $(this).val());
        }
    });

    /****图片预览 开始********/
    Galleria.loadTheme('galleria.classic.min.js');
    ///定义string replaceall方法
    String.prototype.replaceAll = function (s1, s2) {

        return this.replace(new RegExp(s1, "gm"), s2);
    }
    //上传图片按钮事件
    $("#btnsubmitfile").click(function () {
        if ($("#filename").val().length == 0 || $("#thumbnailsname").val().length == 0 || $("#previewname").val().length == 0) {
            alert("请选择图片！");
        }
        else if ($("#filename").val().substring(3).indexOf(",") > -1 || $("#filename").val().substring(3).indexOf(":") > -1
                || $("#thumbnailsname").val().substring(3).indexOf(",") > -1 || $("#thumbnailsname").val().substring(3).indexOf(":") > -1
                || $("#previewname").val().substring(3).indexOf(",") > -1 || $("#previewname").val().substring(3).indexOf(":") > -1
                || $("#filename").val().substring(3).indexOf("，") > -1 || $("#filename").val().substring(3).indexOf("：") > -1
                || $("#thumbnailsname").val().substring(3).indexOf("，") > -1 || $("#thumbnailsname").val().substring(3).indexOf("：") > -1
                || $("#previewname").val().substring(3).indexOf("，") > -1 || $("#previewname").val().substring(3).indexOf("：") > -1) {
            alert("文件名中不能包含逗号或冒号");
        }
        else {
            $("#hidfilename").val($("#hidfilename").val() + $("#filename").val() + ",");
            $("#hidthumbnailsname").val($("#hidthumbnailsname").val() + $("#thumbnailsname").val() + ",");
            $("#hidpreviewname").val($("#hidpreviewname").val() + $("#previewname").val() + ",");
            $("#imgbroswer").find(".galleria-container").remove();

            $("#imgbroswer").html($("#imgbroswer").html() + "<a href='" + $("#filename").val() + "' width='800px' height='300px'><img src='" + $("#filename").val() + "' width='800px' height='300px'/></a>");

            $("#imgbroswer").galleria({ linkSourceImages: false, transition: "fade", transitionSpeed: 1000, width: "500px", height: "400px" });

            $(".galleria-image").click(function () {
                var $imgSrc = $(this).find("img").attr("src");
                var filenamePathArray = $("#hidfilename").val().split(",");
                var previewPathArray = $("#hidpreviewname").val().split(",");
                var previewPath = "";
                for (var i = 0; i < filenamePathArray.length; i++) {
                    if (decodeURI($imgSrc).replaceAll("file:///", "").replaceAll("/", "\\") == decodeURI(filenamePathArray[i])) {
                        previewPath = previewPathArray[i];
                        break;
                    }
                }
                $("#previewimg").html("<img src='file:///" + previewPath + "' width='800px' height='400px'/>").width(800).modal();
            });
            LoadCover();
        }
    });
    /****图片预览 结束********/

    /*******页面验证开始***********/
    $("#form1").validate({
        rules: {
            title: {
                required: true,
                remote: {
                    type: "POST",
                    url: "../AjaxHandler/TourismInfoEditHandler.ashx?t=" + new Date(),
                    data: { title: function () { return $("#title").val(); }, id: code, action: "CheckTitle" }
                }
            },
            tags: { required: true },
            desc: { required: true },
            country: { required: true },
            province: { required: true },
            city: { required: true },
            maptitle: { required: true },
            mapdesc: { required: true },
            map: { required: true },
            pricedan: { required: true, number: true },
            priceunitdan: { required: true },
            seatdan: { required: true, number: true },
            pricewangfan: { required: true, number: true },
            priceunitwangfan: { required: true },
            seatwangfan: { required: true, number: true }

            //                    ,
            //                    filename: { required: true },
            //                    thumbnailsname: { requred: true },
            //                    previewname: { requred: true }

        },
        messages:
                    {
                        title: { required: "请输入标题", remote: "该名称已经存在！" },
                        tags: { required: "请输入关键字！" },
                        desc: { required: "请输入景点描述信息！" },
                        country: { required: "*" },
                        province: { required: "*" },
                        city: { required: "请选择城市" },
                        maptitle: { required: "请输入地图标题" },
                        mapdesc: { required: "*" },
                        map: { required: "*" },
                        pricedan: { required: "请输入单程票价！", number: "请使用数字表示！" },
                        priceunitdan: { required: "请输入单程票价单位！" },
                        seatdan: { required: "请输入单程票票数", number: "请使用数字表示！" },
                        pricewangfan: { required: "请输入往返票价！", number: "请使用数字表示！" },
                        priceunitwangfan: { required: "请输入往返票价单位！" },
                        seatwangfan: { required: "请输入单程票票数！", number: "请使用数字表示！" }
                        //                        ,
                        //                        filename: {required:""},
                        //                        thumbnailsname: {required:"请输入关键字"},
                        //                        previewname: {required:"请输入关键字"},                       
                    }
    });
    /***********页面验证结束**************/

    /******** Tabs 开始*****/
    $("#tabs>li").click(function () {
        var id = $(this).attr("id");
        if (id == "tourinfo") {
            $("div.tourinfo", "#tabs").show();
            $("div.mapInfo", "#tabs").hide();
            $("div.ticketinfo", "#tabs").hide();
            $("div.specialrecommand", "#tabs").hide();
        } else if (id == "mapInfo") {
            $("div.mapInfo", "#tabs").show();
            $("div.tourinfo", "#tabs").hide();
            $("div.ticketinfo", "#tabs").hide();
            $("div.specialrecommand", "#tabs").hide();
        }
        else if (id == "ticketinfo") {
            $("div.ticketinfo", "#tabs").show();
            $("div.tourinfo", "#tabs").hide();
            $("div.mapInfo", "#tabs").hide();
            $("div.specialrecommand", "#tabs").hide();
        } else {
            $("div.ticketinfo", "#tabs").hide();
            $("div.tourinfo", "#tabs").hide();
            $("div.mapInfo", "#tabs").hide();
            $("div.specialrecommand", "#tabs").show();
        }
    });
    /********Tabs 结束*****/

    /**********票价页签开始**********/
    $(".tab-ticket").hover(function () {
        var tid = $(this).attr("id");
        if (tid == "dancheng") {
            $("table.dancheng").show();
            $("table.wangfan").hide();
        } else {
            $("table.wangfan").show();
            $("table.dancheng").hide();
        }
    },
            function () {

            });
    /**********票价页签结束**********/

    //关键字开始
    $("#btnkeywords").click(function () {
        $("#keywordscontainer").css("width", $(document).width() + "px").css("height", $(document).height() + "px").css("left", "0px").toggle(function () {
            $("#selkeywords").load("keywords.html");
            $("#selkeywords").find("#btn_sure").click(function () {

            })
        });

    })

})
